import time

from Common.Base import Base, open_browser

shopping_url = "http://localhost:8080/ecshop/index.php"


# 购物车表现层
class ShoppingPage(Base):
    home_shop = ("css selector", "a[class='cur']")  # 首页
    camera_shop = ("css selector", "img[alt='智能相机']")  # 智能相机
    add_shop = ("css selector", "img[src*='buybtn1.png']")  # 立即购买
    shell_shop = ("css selector", "img[alt='炫彩翻页保护套']")  # 手机壳
    car_shop = ("css selector", "img[alt='平衡车']")  # 平衡车
    deletes_shop = ("css selector", "input[value='清空购物车']")  # 清空购物车
    continue_shop = ("css selector", "img[src*='continue.gif']")  # 继续购物
    update_shop = ("css selector", "input[value='更新购物车']")  # 更新购物车
    settlement_shop = ("css selector", "img[alt='checkout']")  # 结算
    delete_shop = ("css selector", "a[href*='购物车']")  # 删除第一个商品
    modify_shop = ("css selector", "input[name*='goods_number']")  # 修改购买数量

    def click_home(self):
        """
        点击首页操作
        :return:
        """
        self.click(self.home_shop)

    def click_camera(self):
        """
        点击智能相机
        :return:
        """
        self.click(self.camera_shop)

    def click_add(self):
        """
        点击立即购买
        :return:
        """
        self.click(self.add_shop)

    def click_shell(self):
        """
        点击手机壳
        :return:
        """
        self.click(self.shell_shop)

    def click_car(self):
        """
        点击平衡车
        :return:
        """
        self.click(self.car_shop)

    def click_deletes(self):
        """
        清空购物车
        :return:
        """
        self.click(self.deletes_shop)

    def click_continue(self):
        """
        点击继续购物
        :return:
        """
        self.click(self.continue_shop)

    def click_update(self):
        """
        更新购物车
        :return:
        """
        self.click(self.update_shop)

    def click_settlement(self):
        """
        点击结算
        :return:
        """
        self.click(self.settlement_shop)

    def click_delete(self):
        """
        删除单个商品
        :return:
        """
        self.click(self.delete_shop)

    def send_modify(self, number):
        """
        修改商品数量
        :param number:
        :return:
        """
        self.clear(self.modify_shop)
        self.send_keys(self.modify_shop, number)


if __name__ == '__main__':
    driver = open_browser()
    driver.maximize_window()
    shop = ShoppingPage(driver)
    shop.open_url(shopping_url)
    shop.click_camera()
    shop.click_add()
    shop.click_continue()
    shop.click_shell()
    shop.click_add()
    shop.click_delete()
    shop.pop_up_notification()
    time.sleep(5)
    shop.close_browser()
